package com.xuecheng.system.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @author Tim Hooper
 * @version 1.0
 * @time 2023/01/31/1:55
 */
@Configuration
public class GlobalCrossFilter {

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfig = new CorsConfiguration();
        //可放行的源
        corsConfig.addAllowedOrigin("*");
        //可放行的请求方法
        corsConfig.addAllowedMethod("*");
        corsConfig.addExposedHeader("*");
        corsConfig.setAllowCredentials(true);
        configSource.registerCorsConfiguration("/**", corsConfig);
        return new CorsFilter(configSource);
    }
}
